package guda.house.web.action.bill;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.BillWaterBiz;
import guda.house.biz.BillWaterElecBiz;
import guda.house.biz.OwnerBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.enums.WaterElecTypeEnum;
import guda.house.biz.form.bill.*;
import guda.house.biz.query.BillWaterQuery;
import guda.house.biz.query.WaterElecQuery;
import guda.house.biz.security.AppUtil;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dao.BillElecDAO;
import guda.house.dal.dao.BillWaterDAO;
import guda.house.dal.dataobject.*;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by well on 16/1/21.
 */
@Controller
@RequestMapping(value = "bill/waterElec")
public class BillWaterElecAction {

    @Autowired
    private BillWaterBiz billWaterBiz;
    @Autowired
    private OwnerBiz ownerBiz;
    @Autowired
    private BillWaterDAO billWaterDAO;
    @Autowired
    private BillElecDAO billElecDAO;
    @Autowired
    private BillWaterElecBiz billWaterElecBiz;

    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap,WaterElecSearchForm waterElecSearchForm) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        if(waterElecSearchForm.getVillageId() == null) {
            List<Long> villageIdList = AppUtil.getVillageIdList();
            if (villageIdList.size() > 0) {
                waterElecSearchForm.setVillageId(villageIdList.get(0));
            }
        }
        WaterElecQuery waterElecQuery = new WaterElecQuery();
        waterElecQuery.setPageNo(pageId);
        waterElecQuery.setPageSize(pageSize);
        waterElecQuery.setVillageId(waterElecSearchForm.getVillageId());
        waterElecQuery.setSuiteName(waterElecSearchForm.getSuiteName());
        if(StringUtils.hasText(waterElecSearchForm.getType())) {
            waterElecQuery.setType(waterElecSearchForm.getType());
        }else{
            waterElecQuery.setType(WaterElecTypeEnum.water.name());
        }
        modelMap.put("query", waterElecQuery);
        BizResult bizResult = billWaterElecBiz.list(waterElecQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "bill/waterElec/list.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }
    }

    @RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, BillWaterElecEditForm billWaterElecEditForm,
                       BindingResult result, Map<String, Object> model) {

        BizResult bizResult = billWaterElecBiz.queryForEdit(billWaterElecEditForm);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "bill/waterElec/edit.vm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }


    @RequestMapping(value = "/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, BillWaterElecForm billWaterElecForm,
                         BindingResult result) {
        billWaterElecForm.setResetZero(0);
        return "bill/waterElec/create.vm";
    }

    @RequestMapping(value = "/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap, @Valid BillWaterElecForm billWaterElecForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            BizResult bizResult = ownerBiz.queryForCreateReset(billWaterElecForm.getVillageId(), billWaterElecForm.getBuildingId(), billWaterElecForm.getUnitId());
            modelMap.putAll(bizResult.data);
            return "bill/waterElec/create.vm";
        }
        if (billWaterElecForm.getRecordCurrent() - billWaterElecForm.getRecordLast() < 0) {
            result.rejectValue("recordCurrent", "recordCurrentError", "本月读数应该大于上月读数");
            BizResult bizResult = ownerBiz.queryForCreateReset(billWaterElecForm.getVillageId(), billWaterElecForm.getBuildingId(), billWaterElecForm.getUnitId());
            modelMap.putAll(bizResult.data);
            return "bill/waterElec/create.vm";
        }

        BizResult bizResult = billWaterElecBiz.create(billWaterElecForm);
        if (bizResult.success) {
            return "redirect:/bill/waterElec/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap, @Valid BillWaterElecEditForm billWaterElecEditForm,
                           BindingResult result, Map<String, Object> model) {
        if (result.hasErrors()) {
            return "bill/waterElec/edit.vm";
        }
        BizResult bizResult = billWaterElecBiz.update(billWaterElecEditForm);
        if (bizResult.success) {
            return "redirect:/bill/waterElec/list.htm";
        } else {
            return "redirect:/error.htm?code=" + bizResult.code;
        }

    }

    @RequestMapping(value = "/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = billWaterBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }

    @RequestMapping(value = "/getLatest.htm",method = RequestMethod.POST)
    public void getLatest(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long suiteId = RequestUtil.getLong(request, "suiteId");
        String type = request.getParameter("type");
        if(WaterElecTypeEnum.elec.name().equals(type)){
            BillElecDO billElecDO = billElecDAO.queryLatest(suiteId);
            if(billElecDO == null){
                billElecDO.setRecordCurrent(0);
                ResponseHelper.ajaxReturn(billElecDO, response);
                return;
            }
            if(billElecDO.getResetZero()!= null && billElecDO.getResetZero().intValue() ==1){
                billElecDO.setRecordCurrent(0);
            }
            ResponseHelper.ajaxReturn(billElecDO, response);
            return;
        }else if(WaterElecTypeEnum.water.name().equals(type)) {
            BillWaterDO billWaterDO = billWaterDAO.queryLatest(suiteId);
            if(billWaterDO == null){
                billWaterDO.setRecordCurrent(0);
                ResponseHelper.ajaxReturn(billWaterDO, response);
                return;
            }
            if(billWaterDO.getResetZero()!= null && billWaterDO.getResetZero().intValue() ==1){
                billWaterDO.setRecordCurrent(0);
            }
            ResponseHelper.ajaxReturn(billWaterDO, response);
            return;
        }
        JsonResult jsonResult = JsonResult.getInstance();
        jsonResult.setErrMsg(CommonResultCode.UNKOWN_ERROR);
        ResponseHelper.ajaxReturn(jsonResult.getMap(), response);
        return;
    }
}
